﻿// //  ********************************************************************************
// //  Copyright (C) 2016 Sebastian Kenter. All rights reserved.
// // 
// //  Name    : PhotonServerProject | PhotonServerCommon | MessageSerializer.cs
// //  Created : 2016 - 04 - 15 12:13
// //  Modified: 2016 - 04 - 18 09:38
// //  ********************************************************************************

using System.IO;
using Google.Protobuf;

namespace PhotonServerCommon.Messages.Helper
{
    public static class MessageSerializer
    {
        public static byte[] SerializeMessage(IMessage message)
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                message.WriteTo(outStream);
                return outStream.ToArray();
            }
        }

        public static T DeserializeMessage<T>(byte[] inputStream) where T : class, IMessage<T>, new()
        {
            T result = new T();

            result.MergeFrom(inputStream);

            return result;
        }
    }
}
